package gsp420.helloworld;

public class ThreadingExample extends Prime implements Runnable {

	int m_num;
	char m_letter;
	/**
	 * the main function to run for this class is {@link #doThings()}
	 * @param a_letterToPrint
	 * @param a_num
	 */
	ThreadingExample(char a_letterToPrint, int a_num){
		m_num = a_num;
		m_letter = a_letterToPrint;
	}
	
	/** an example of how to overwrite a function. see {@link Prime#isPrime(int)} */
	public boolean isPrime(int a){
		return super.isPrime(a);
	}
	
	/** will print out {@link #m_letter}, {@link #m_num} times */
	void doThings() throws IllegalStateException {
		if(m_num < 0){
			throw new IllegalStateException("m_num is "+m_num);
		}
		for(int i = 0; i < m_num; ++i)
		{
			System.out.print(m_letter);
			try{
				Thread.sleep(1);
			}catch(Exception e){}
		}
	}
	
	public static void main(String[] args){
		
		int a, b;
		System.out.println("dividing 2 numbers... ");
		try{
			a = Integer.parseInt(args[0]);
			b = Integer.parseInt(args[1]);
			System.out.println(a+" / "+b);
			int result = a/b;
			System.out.println(result);
		}catch(ArrayIndexOutOfBoundsException e){
			System.out.println("You are missing arguments!");
		}catch(Exception e){
			System.out.println("you done something wrong.");
			e.printStackTrace();
		}
		final int COUNT = 5;
		// create room for threads
		Thread[] threads = new Thread[COUNT];
		// allocate threads with objects to thread
		for(int i = 0; i < threads.length; ++i){
			threads[i] = new Thread(new ThreadingExample((char)('A'+i), 100));
		}
		// start the threads (which will call run on every object in a separate thread)
		for(int i = 0; i < threads.length; ++i){
			threads[i].start();
		}
	}
	
	public void run(){
		doThings();
		// example of how to make your own exception
		try{
			throw new Exception("I want a stack trace");
		}catch(Exception e){
			System.out.println();
			e.printStackTrace();
		}
	}
}
